import matplotlib.pyplot as plt
import sklearn.datasets as sd
import pandas as pd
import sklearn.model_selection as ms
import sklearn.linear_model as lm
import sklearn.metrics as sm
import sklearn.pipeline as pl
import sklearn.preprocessing as sp
import sklearn.tree as st
import sklearn.ensemble as se
data = sd.load_boston()
# print(data.keys())
# print(data.filename) #/home/tarena/.local/lib/python3.10/site-packages/sklearn/datasets/data/boston_house_prices.csv
# print(data.data.shape)##x
# print(data.target.shape)##Y

x = data.data
y = data.target
# train_list = []
# test_list = []
# idx = 1
# for i in x:
#     if idx % 10 ==0:
#         test_list.append(i)
#     else:
#         train_list.append(i)
train_x,\
test_x,\
train_y,\
test_y=ms.train_test_split(x,y,test_size=0.2,random_state=7)

#线性回归
model=lm.LinearRegression()
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('训练r2:',sm.r2_score(train_y,pred_train_y))
print('测试r2:',sm.r2_score(test_y,pred_test_y))

#多项式回归
model = pl.make_pipeline(sp.PolynomialFeatures(2),
                         lm.LinearRegression())
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('多项式训练r2:',sm.r2_score(train_y,pred_train_y))
print('多项式测试r2:',sm.r2_score(test_y,pred_test_y))

#决策树回归
model = st.DecisionTreeRegressor(max_depth=6)
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('决策树训练r2:',sm.r2_score(train_y,pred_train_y))
print('决策树测试r2:',sm.r2_score(test_y,pred_test_y))

#Adaboost
model = st.DecisionTreeRegressor(max_depth=5)
model = se.AdaBoostRegressor(model,n_estimators=400,
                             random_state=7)
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('Adaboost训练r2:',sm.r2_score(train_y,pred_train_y))
print('Adaboost测试r2:',sm.r2_score(test_y,pred_test_y))

#特征重要性
# fi = model.feature_importances_
# fi = pd.Series(fi,index=data.feature_names)
# fi = fi.sort_values()
# fi.plot.bar(rot=45)
# plt.show()

#GBDT
model = se.GradientBoostingRegressor(max_depth=4,
                                     n_estimators=400,
                                     min_impurity_split=5)
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('GBDT训练r2:',sm.r2_score(train_y,pred_train_y))
print('GBDT测试r2:',sm.r2_score(test_y,pred_test_y))

#随机森林RF
model = se.RandomForestRegressor(max_depth=4,
                                 n_estimators=400,
                                 min_samples_split=5)
model.fit(train_x,train_y)
pred_test_y=model.predict(test_x)
pred_train_y=model.predict(train_x)
print('RF训练r2:',sm.r2_score(train_y,pred_train_y))
print('RF测试r2:',sm.r2_score(test_y,pred_test_y))




